<?php

# Simple SIAKAD
# Aplikasi Sistem Informasi Akademik untuk PT/PTS Indonesia
# 
# version 1.0
# class setting
# file models/setting.php
# created Nov 23, 2012 8:14:57 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Setting extends SI_Model {        
    
    public function force_chg_pass($id, $pass){
        $data = array('pass_pengguna'=>md5($pass));
        $this->db->where('id_pengguna',$id);
        $this->db->update('pengguna', $data);
    }
        
    public function change_pass($id, $post = array()){
        $rules = array(
            array('field' => 'old_pass', 'label' => 'Password Lama', 'rules' => 'required|trim'),
            array('field' => 'new_pass', 'label' => 'Password Baru', 'rules' => 'required|trim|min_length[6]|alpha_numeric'),
            array('field' => 'konf_pass', 'label' => 'Konfirmasi Password', 'rules' => 'matches[new_pass]'),            
        );    
        
        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;                        
            return false;
        }
        
        if(!$this->_check_pass($id, $post['old_pass'])){
            $this->umsg = "Password lama tidak sesuai";                        
            return false;
        }
        
        $this->db->where('id_pengguna', $id);
        return $this->db->update('pengguna', array('pass_pengguna'=>md5($post['new_pass'])));
        
    }
    
    public function get_users($id=''){
        if($id!='') $this->db->where('id_pengguna',$id);
        $que = $this->db->get('pengguna');
        
        if($id!='') return $que->row_array();
        else return $que->result_array();
    }
    
    public function insert($data = array()){
        $rules = array(
            array('field' => 'nama_lengkap', 'label' => 'Nama Pengguna', 'rules' => 'required|trim'),
            array('field' => 'user_pengguna', 'label' => 'Username', 'rules' => 'required|alpha_numeric|trim'),
            array('field' => 'pass_pengguna', 'label' => 'Password', 'rules' => 'required|alpha_numeric|min_length[6]|trim')
        );    
        
        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;                        
            return false;
        }
        
        if($this->_check_user($data['user_pengguna'])){
            $this->umsg = "User Login sudah terdaftar, gunakan user lain";                        
            return false;
        }
        
        $data['pass_pengguna'] = md5($data['pass_pengguna']);
        $data['tanggal_log'] = date('Y-m-d H:i:s');
        $data['tanggal_reg'] = date('Y-m-d H:i:s');
        $data['level_pengguna'] = 2;
        $data['status_pengguna'] = 1;
        
        if($this->db->insert('pengguna', $data)) 
            return true;
        else return false;
               
    }
    
    public function update($id, $data = array()){
        $rules = array(
            array('field' => 'nama_lengkap', 'label' => 'Nama Pengguna', 'rules' => 'required|trim'),
            array('field' => 'user_pengguna', 'label' => 'Username', 'rules' => 'required|alpha_numeric|trim'),
            array('field' => 'status_pengguna', 'label' => 'Password', 'rules' => 'required|numeric')
        );  
        
        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;                        
            return false;
        }
                
        $data['tanggal_update'] = date('Y-m-d H:i:s');        
        
        $this->db->where('id_pengguna',$id);
        if($this->db->update('pengguna', $data)) 
            return true;
        else {
            $this->umsg = "User Login sudah terdaftar, gunakan user lain";
            return false;            
        }
    }
    
    private function _check_user($usr){
        $this->db->where('user_pengguna',$usr);
        $que = $this->db->get('pengguna');
        
        $rst = $que->num_rows();
        if($rst>0) return true;
        else return false;
    }
    
    private function _check_pass($id, $pass){
        $this->db->where('id_pengguna',$id);
        $que = $this->db->get('pengguna');
        
        $rst = $que->row_array();
        if($rst['pass_pengguna']==md5($pass))
            return true;
        else return false;
    }
}
